import { _decorator, Component, Node } from 'cc';
import { BaseView } from '../BaseView';
import { UIMgr } from '../UIMgr';
import { Layer } from '../Layer';
import { ViewLayerDefine } from '../VewLayerDefine';
const { ccclass, property } = _decorator;

@ccclass('LoadingLayer')
export class LoadingLayer extends Layer {



    show(view: BaseView) {
        let exitView:BaseView|null = null;
        this.views.forEach(item => {
            if(item===view){
                exitView = item;
            }
        });
        if(exitView==null){
            UIMgr.Ins.hideLayer(ViewLayerDefine.Game)
            UIMgr.Ins.hideLayer(ViewLayerDefine.Main)
            UIMgr.Ins.hideLayer(ViewLayerDefine.Pop)
            this.views.push(view);
            this.node.addChild(view.node)
            view.node.setPosition(0, 0);
            try {
                view.node.setSiblingIndex(this.node.children.length - 1);
                if(!view.isInit){
                    view.initControls();//初始化控件
                    view.init();
                    view.isInit = true;
                }
                view.beforeShow()
                view.show();
                view.afterShow();
            } catch (e) {
                console.error(e)
            }
            return
        }
        if(exitView.node.active){
            return;
        }

        UIMgr.Ins.hideLayer(ViewLayerDefine.Game)
        UIMgr.Ins.hideLayer(ViewLayerDefine.Main)
        UIMgr.Ins.hideLayer(ViewLayerDefine.Pop)
        try {
            view.node.setSiblingIndex(this.node.children.length - 1);
            view.beforeShow()
            view.show();
            view.afterShow();
        } catch (e) {
            console.error(e)
        }
    }

    hide(view: BaseView) {
        try {
            view.beforeHide();
            view.hide();
            view.afterHide();
        } catch (e) {
            console.error(e)
        }
    }
}


